Skip to main content
  1. Writing/

Uploading Nest Camera Video To Azure Storage

·710 words

Whale breaching water

(Image sourced from StockIO.com.)

Last weekend I hacked together a solution that allows Nest stream capture locally. This weekend I got a chance to improve it a bit and make the entire solution cloud-ready.

There is a number of changes in this release:

  • Parallelization of camera captures. If you have many cameras, you will not experience a video lag in your captures - previously, the streams were chained, now they work at the same time for different devices, so videos are produced simultaneously.
  • High-quality images. Image quality is much better now, compared to the previous version. Less pixelated means now you have a better idea who is moving around in front of your camera.
  • Support for Azure Storage upload. Now, you can capture videos and upload them to Azure Storage.
  • Support for container deploy. foggycam now supports Docker out-of-the-box, so you no longer have to clone the tooling every time you need to run it - just package a Docker container, and push it whenever you need it.
  • Support for pre-installed ffmpeg. If you have ffmpeg installed on your Linux or Mac box, we identify it and use it, so you no longer have to manually install and copy it locally if you don’t want to.

Azure Storage Functionality #

The interesting part about this update, at least in my own humble opinion, is the support for Azure Storage uploads and the Docker-ization of the deployment. If you don’t yet have an Azure account, make sure you sign up (free trial available) before you take advantage of that functionality.

The upload happens with the help of the Azure Storage Python SDK:

"""Provides a way to upload video content to Azure Storage."""

from azure.storage.blob import BlockBlobService, ContentSettings

class AzureStorageProvider(object):
    """Class that facilitates connection to Azure Storage."""

    def upload_video(self, account_name='', sas_token='', container='', blob='', path=''):
        """Upload video to the provided account."""

        block_blob_service = None

        if account_name and sas_token and container and blob:
            block_blob_service = BlockBlobService(account_name=account_name, sas_token=sas_token)
            containers = block_blob_service.list_containers()

            if container not in containers:
                block_blob_service.create_container(container)
        else:
            print 'ERROR: No account credentials for Azure Storage specified.'

        block_blob_service.create_blob_from_path(
            container,
            blob,
            path,
            content_settings=ContentSettings(content_type='video/mp4')
        )

The sas_token and account_name are read in from the config file, and then for each registered camera a new blob container is created. Once the video is generated, a setting is read in from the configuration file that determines whether the video should be pushed to Azure Storage.

NOTE: Currently the code does no checks on Azure Storage usage, so make sure you are aware of your account limits.

Container Deployment #

You can now deploy foggycam with the help of Docker containers. When you clone the repo, you will now gain access to a Dockerfile. Make sure you set all the values in the config.json file before you build the container.

When ready to build it, simply call:

docker build -t foggycam_image .

This will build the image. Once the build is complete, you can run:

docker run -it foggycam_image 

The cool thing about having a Docker image is that you can also deploy it to Azure and have it run outside the boundaries of your local machine - that also ensures that the capture happens even when your machine is not running.

If you already have the Azure CLI installed, run the following command to authenticate:

az login

Through the portal, create a new Azure Resource Group and an Azure Container Registry instance. Once done, tag the image you just created with the URL to the container instance:

docker tag foggycam_image <azure_cr_instance>.azurecr.io/infrastructure     

Now, make sure you log in to the container registry via the terminal:

az acr login --name <azure_cr_instance> 

Push the freshly-tagged image to the container registry:

docker push <azure_cr_instance>.azurecr.io/infrastructure

And finally, provision the Azure Container Instance:

az container create --resource-group <your_resource_group> --name foggycam-container --image <azure_cr_instance>.azurecr.io/infrastructure:latest --ip-address public --port 80 --registry-password <azure_cr_instance_password>

Once the container instance is running, the video content will be generated and uploaded to the associated storage account.

NOTE: Something to keep in mind is that in the context of this work, I have not put in place any explicit security boundaries - as you deploy containers, consider the fact that you are deploying a configuration file with the credentials into the container. I recommend using a tool like Azure Key Vault to control key access.